package com.zeng.sboot.rt;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * CMD 显示字体是 GBK | GB2312; UTF8 出现乱码
 */
public class ProcessBuilderDemo {

    public void test1() {
        String chartSet = "gb2312";//"gbk"
        ProcessBuilder pb = new ProcessBuilder();
        //pb.command("ipconfig");//OK

        //pb.command("ipconfig /all");//java.io.IOException: Cannot run program "ipconfig /all": CreateProcess error=2, 系统找不到指定的文件。
        List<String> c = Arrays.asList("ipconfig", "/all");//OK
        pb.command(c);

        try {
            Process process = pb.start();

            InputStream is = process.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is, chartSet));
            //byte[] b = new byte[1024];
            String rc = null;
            while ((rc = br.readLine()) != null) {
                //String nrc = new String(rc.getBytes("ISO-8859-1"), "UTF-8");
                //String nrc = new String(rc.getBytes(chartSet), "UTF-8");//乱码
                System.out.println(rc);
            }

            is.close();
            br.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void test2() {
        ProcessBuilder pb = new ProcessBuilder();

        List<String> command = Arrays.asList("C:\\Users\\zengzhijun\\Desktop\\aliyun-oss\\ossutil64\\ossutil64.exe","ls");
        pb.command(command);

        try {
            Process p = pb.start();
            InputStream is = p.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            byte[] b = new byte[1024];
            while (bis.read(b) != -1) {
                System.out.println(new String(b,0,b.length));
            }
            is.close();
            bis.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //oss://fapd-industrial-engineering-department/sss/
    public void test3() {
        ProcessBuilder pb = new ProcessBuilder();

        List<String> command = new ArrayList<>();
        command.add("C:\\Users\\zengzhijun\\Desktop\\aliyun-oss\\ossutil64\\ossutil64.exe");
        command.add("cp");
        command.add("E:/syuka.JPG");
        command.add("oss://fapd-industrial-engineering-department/sss/");
        pb.command(command);

        try {
            Process p = pb.start();
            InputStream is = p.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            byte[] b = new byte[1024];
            while (bis.read(b) != -1) {
                System.out.println(new String(b,0,b.length));
            }
            is.close();
            bis.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }




    public static void main(String[] args) {
        ProcessBuilderDemo demo = new ProcessBuilderDemo();

        //demo.test1();
        //demo.test2();
        demo.test3();
    }

}
